package cn.wolfcde.luowowo.website.web.controller;

import cn.wolfcde.luowowo.website.annotation.RedisParameter;
import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Scenic;
import cn.wolfcode.luowowo.article.domain.ScenicContent;
import cn.wolfcode.luowowo.article.query.ScenicQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IScenicContentService;
import cn.wolfcode.luowowo.article.service.IScenicService;
import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.ScenicCommentQuery;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import cn.wolfcode.luowowo.common.domain.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("scenic")
public class ScenicController {
    @Reference
    private IScenicCommentService scenicCommentService;
    @Reference
    private IScenicService scenicService;

    @Reference
    private IScenicContentService scenicContentService;
    @Reference
    private IDestinationService destinationService;

    /**
     * 具体某个景点的回显
     * @param scenicId
     * @param model
     * @return
     */
    @RequestMapping("scenicDetail")
    public String scenicDetail(Long scenicId ,Model model){
        Scenic scenic =scenicService.selectById(scenicId);
        ScenicContent content=scenicContentService.selectByScenicId(scenicId);
        scenic.setContent(content.getContent());
        model.addAttribute("scenic",scenic);

        List<Destination> toasts = destinationService.getToasts(scenic.getDestId());
        toasts.remove(toasts.size() - 1);
        //toast
        model.addAttribute("toasts", toasts);
        //dest
        Destination destination = destinationService.selectById(scenic.getDestId());
        model.addAttribute("dest", destination);
        //scenics
        ScenicQuery qo = new ScenicQuery();
        PageInfo<Scenic> pageInfo= scenicService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        //list
        ScenicCommentQuery scenicqo = new ScenicCommentQuery();
        scenicqo.setScenicId(scenicId);
        qo.setPageSize(Integer.MAX_VALUE);
        Page<ScenicComment> query = scenicCommentService.query(scenicqo);
        model.addAttribute("query",query.getContent());
        return "scenic/scenicDetail";
    }

    /**
     * 新增景点评论
     * @param scenicComment
     * @param userInfo
     * @return
     */
    @RequestMapping("CommentSave")
    @ResponseBody
    public AjaxResult scenicCommentSave(ScenicComment scenicComment, @RedisParameter UserInfo userInfo){
        AjaxResult ajaxResult = new AjaxResult();

        if (scenicComment.getComment()=="" || scenicComment.getComment()==null) {
            ajaxResult.mark("评论不能为空");
            return ajaxResult;
        }
        scenicComment.setHeadUrl(userInfo.getHeadImgUrl());
        scenicComment.setUserId(userInfo.getId());
        scenicComment.setLevel(userInfo.getLevel());
        scenicComment.setUsername(userInfo.getNickname());
        scenicCommentService.commentAdd(scenicComment);

        return ajaxResult;
    }

    @RequestMapping("scenicThumbup")
    @ResponseBody
    public AjaxResult commentThumbUp(String id, @RedisParameter UserInfo userInfo) {

        AjaxResult ajaxResult = new AjaxResult();

          scenicCommentService.strategyThumbup(id, userInfo.getId());

        //ajaxResult.setResult(strategyComment);

        return ajaxResult;
    }


}
